package com.hspedu.smallchange.oop;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * 该类是完成零钱通的各个功能的类
 * 使用oop编程(面向对象编程)
 * 将各个功能对应一个方法
*/
public class SmallChangeOOP {
    private String details = "---------------零钱通明细---------------";
    private boolean loop = true;
    private double money = 0;   //消费或者收入
    private double balance = 0; //余额
    Scanner sc = new Scanner(System.in);
    Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");//设置日期格式

    //先显示菜单，并可以进行选择
    public void menu(){
        System.out.println("---------------零钱通菜单---------------");
        System.out.println("\t\t\t1 零钱通明细");
        System.out.println("\t\t\t2 收益入账");
        System.out.println("\t\t\t3 消费");
        System.out.println("\t\t\t4 退  出");
        System.out.print("请选择(1-4)：");
    }

    //进行菜单的选择
    public void start(){
        String key = "";    //用户选择的数
        while(loop){
            this.menu();
            key = sc.next();
            switch (key){
                case "1":
                    System.out.println(this.details);
                    break;
                case "2":
                    System.out.println(this.income());
                    break;
                case "3":
                    System.out.println(this.consume());
                    break;
                case "4":
                    this.quit();
                    break;
                default:
                    System.out.println("选择有误，请输入(1~4)进行选择");
                    break;
            }
        }
    }

    /**
     * 收益入账
     */
    public String income(){
        money = 0;
        //进行入账金额校验,需要>0
        System.out.print("入账金额：");
        while(true){
            money = sc.nextDouble();
            if(money <= 0){
                System.out.println("正确入账金额需>0");
                System.out.print("请重新输入入账金额：");
                continue;
            }
            break;
        }

        //把余额进行累加，并将格式如 收入入账 +金额 时间 余额的字符串进行拼接，拼接到details里面去
        date = new Date();
        balance +=money;
        String temp = "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t余额" + balance;
        details += temp;
        return "收益入账\t+" + money + "\t" + sdf.format(date) + "\t余额" + balance;
    }


    /**
     * consume
     * 功能：从键盘读入消费的金额，并以特定的格式拼接到details中，并返回这个字符串
     */
    public String consume(){
        money = 0;
        String note = "";

        //对消费金额进行校验，直到输入的消费金额正确
        while(true){
            System.out.print("消费金额：");
            money = sc.nextDouble();
            if (money <= 0 || money > balance){
                System.out.println("消费金额应在 1~"+balance + " 之间");
                System.out.print("请重新输入消费金额：");
                continue;
            }
            break;
        }

        System.out.print("消费说明：");
        note=sc.next();
        date = new Date();
        balance -=money;
        String temp = "\n"+note +"\t-" + money + "\t" + sdf.format(date) + "\t余额" + balance;
        details += temp;
        return note +"\t-" + money + "\t" + sdf.format(date) + "\t余额" + balance;
    }


    /**
     * quit
     * 功能：确认退出，y/n，如果是y 则将传进来的Boolean值loop 设为false
     */
    public void quit(){
        System.out.print("是否确定退出系统(y/n)：");
        String key = "";//用户的输入

        //进行对输入字符串key的校验
        while(true){
            key = sc.next();
            if (key.equals("y")||key.equals("n")){
                break;
            }
            System.out.print("输入有误请重新输入(y/n):");
        }

        if (key.equals("y")){
            loop = false;
            System.out.println("程序退出，欢迎下次使用！");
        }else{
            System.out.println("程序继续运行~~");
        }
    }
}
